library(plyr)
library(igraph)
library(fmsb)
library(Hmisc)
library(MASS)
library(stargazer)
library(tidyverse)
library(multcomp)
library(gsynth)
data(gsynth)
library(panelView)
library(modelsummary) # https://modelsummary.com/articles/modelsummary.html#new-models-and-custom-statistics

setwd("~/Dropbox/China Huawei Paper/Production Materials/Replication/")
master.data <- read.csv("dataFile_China_Huawei.csv"); head(master.data)


# min.T0 = 3
dat <- master.data
dat$treated = 0
threshold=1000000
dat = as.data.frame(dat %>%
  group_by(cabb) %>%
  dplyr::mutate(treated = ifelse(row_number() == 1, 0, NA),
         treated = ifelse(china.huawei > threshold, 1, treated)) %>%
  fill(treated))
dat$treated[is.na(dat$treated)==T] <- 0

sub = dat[dat$v2x_polyarchy>0.42,]
sub = sub[sub$cabb!="MNG",]
sub = sub[sub$cabb!="SSD",]
sub = sub[is.na(sub$v2smgovfilprc)==F & is.na(sub$treated)==F & is.na(sub$v2x_polyarchy)==F & is.na(sub$pres.election)==F & is.na(sub$coup.attempts)==F & is.na(sub$successful.coups)==F,]
sub = sub[is.na(sub$pcGDPcur)==F & is.na(sub$GDPcur)==F & is.na(sub$electricitypc)==F,]
nrow(dat);nrow(sub)

out1 <- gsynth(v2smgovfilprc ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=3, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out1, type="gap", xlim=c(-5,10), ylim=c(-0.2,1.0), main="Democracies\nSocial Media Filtering")
out1$est.avg
out1Periods3 = out1$est.avg

out2 <- gsynth(v2smgovshut ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=3, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out2, type="gap", xlim=c(-5,10), ylim=c(-0.2,1.0), main="Democracies\nInternet Shutdowns")
out2$est.avg
out2Periods3 = out2$est.avg

out3 <- gsynth(v2smgovsmmon ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=3, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out3, type="gap", xlim=c(-5,10), ylim=c(-0.2,1.0), main="Democracies\nSocial Media Monitoring")
out3$est.avg
out3Periods3 = out3$est.avg

out4 <- gsynth(v2smarrest ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=3, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out4, type="gap", xlim=c(-5,10), ylim=c(-0.2,1.0), main="Democracies\nArrests for Online Content")
out4$est.avg
out4Periods3 = out4$est.avg


# min.T0 = 4
dat <- master.data
dat$treated = 0
threshold=1000000
dat = as.data.frame(dat %>%
  group_by(cabb) %>%
  dplyr::mutate(treated = ifelse(row_number() == 1, 0, NA),
         treated = ifelse(china.huawei > threshold, 1, treated)) %>%
  fill(treated))
dat$treated[is.na(dat$treated)==T] <- 0

sub = dat[dat$v2x_polyarchy>0.42,]
sub = sub[sub$cabb!="MNG",]
sub = sub[sub$cabb!="SSD",]
sub = sub[is.na(sub$v2smgovfilprc)==F & is.na(sub$treated)==F & is.na(sub$v2x_polyarchy)==F & is.na(sub$pres.election)==F & is.na(sub$coup.attempts)==F & is.na(sub$successful.coups)==F,]
sub = sub[is.na(sub$pcGDPcur)==F & is.na(sub$GDPcur)==F & is.na(sub$electricitypc)==F,]
nrow(dat);nrow(sub)

out1 <- gsynth(v2smgovfilprc ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=4, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out1, type="gap", xlim=c(-5,10), ylim=c(-0.2,1.0), main="Democracies\nSocial Media Filtering")
out1$est.avg
out1Periods4 = out1$est.avg

out2 <- gsynth(v2smgovshut ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=4, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out2, type="gap", xlim=c(-5,10), ylim=c(-0.2,1.0), main="Democracies\nInternet Shutdowns")
out2$est.avg
out2Periods4 = out2$est.avg

out3 <- gsynth(v2smgovsmmon ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=4, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out3, type="gap", xlim=c(-5,10), ylim=c(-0.2,1.0), main="Democracies\nSocial Media Monitoring")
out3$est.avg
out3Periods4 = out3$est.avg

out4 <- gsynth(v2smarrest ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=4, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out4, type="gap", xlim=c(-5,10), ylim=c(-0.2,1.0), main="Democracies\nArrests for Online Content")
out4$est.avg
out4Periods4 = out4$est.avg


# min.T0 = 6
dat <- master.data
dat$treated = 0
threshold=1000000
dat = as.data.frame(dat %>%
  group_by(cabb) %>%
  dplyr::mutate(treated = ifelse(row_number() == 1, 0, NA),
         treated = ifelse(china.huawei > threshold, 1, treated)) %>%
  fill(treated))
dat$treated[is.na(dat$treated)==T] <- 0

sub = dat[dat$v2x_polyarchy>0.42,]
sub = sub[sub$cabb!="MNG",]
sub = sub[sub$cabb!="SSD",]
sub = sub[is.na(sub$v2smgovfilprc)==F & is.na(sub$treated)==F & is.na(sub$v2x_polyarchy)==F & is.na(sub$pres.election)==F & is.na(sub$coup.attempts)==F & is.na(sub$successful.coups)==F,]
sub = sub[is.na(sub$pcGDPcur)==F & is.na(sub$GDPcur)==F & is.na(sub$electricitypc)==F,]
nrow(dat);nrow(sub)

out1 <- gsynth(v2smgovfilprc ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=6, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out1, type="gap", xlim=c(-5,10), ylim=c(-0.2,1.0), main="Democracies\nSocial Media Filtering")
out1$est.avg
out1Periods6 = out1$est.avg

out2 <- gsynth(v2smgovshut ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=6, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out2, type="gap", xlim=c(-5,10), ylim=c(-0.2,1.0), main="Democracies\nInternet Shutdowns")
out2$est.avg
out2Periods6 = out2$est.avg

out3 <- gsynth(v2smgovsmmon ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=6, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out3, type="gap", xlim=c(-5,10), ylim=c(-0.2,1.0), main="Democracies\nSocial Media Monitoring")
out3$est.avg
out3Periods6 = out3$est.avg

out4 <- gsynth(v2smarrest ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=6, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out4, type="gap", xlim=c(-5,10), ylim=c(-0.2,1.0), main="Democracies\nArrests for Online Content")
out4$est.avg
out4Periods6 = out4$est.avg


# min.T0 = 7
dat <- master.data
dat$treated = 0
threshold=1000000
dat = as.data.frame(dat %>%
  group_by(cabb) %>%
  dplyr::mutate(treated = ifelse(row_number() == 1, 0, NA),
         treated = ifelse(china.huawei > threshold, 1, treated)) %>%
  fill(treated))
dat$treated[is.na(dat$treated)==T] <- 0

sub = dat[dat$v2x_polyarchy>0.42,]
sub = sub[sub$cabb!="MNG",]
sub = sub[sub$cabb!="SSD",]
sub = sub[is.na(sub$v2smgovfilprc)==F & is.na(sub$treated)==F & is.na(sub$v2x_polyarchy)==F & is.na(sub$pres.election)==F & is.na(sub$coup.attempts)==F & is.na(sub$successful.coups)==F,]
sub = sub[is.na(sub$pcGDPcur)==F & is.na(sub$GDPcur)==F & is.na(sub$electricitypc)==F,]
nrow(dat);nrow(sub)

out1 <- gsynth(v2smgovfilprc ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=7, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out1, type="gap", xlim=c(-5,10), ylim=c(-0.2,1.0), main="Democracies\nSocial Media Filtering")
out1$est.avg
out1Periods7 = out1$est.avg

out2 <- gsynth(v2smgovshut ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=7, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out2, type="gap", xlim=c(-5,10), ylim=c(-0.2,1.0), main="Democracies\nInternet Shutdowns")
out2$est.avg
out2Periods7 = out2$est.avg

out3 <- gsynth(v2smgovsmmon ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=7, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out3, type="gap", xlim=c(-5,10), ylim=c(-0.2,1.0), main="Democracies\nSocial Media Monitoring")
out3$est.avg
out3Periods7 = out3$est.avg

out4 <- gsynth(v2smarrest ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=7, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out4, type="gap", xlim=c(-5,10), ylim=c(-0.2,1.0), main="Democracies\nArrests for Online Content")
out4$est.avg
out4Periods7 = out4$est.avg


# min.T0 = 8
dat <- master.data
dat$treated = 0
threshold=1000000
dat = as.data.frame(dat %>%
  group_by(cabb) %>%
  dplyr::mutate(treated = ifelse(row_number() == 1, 0, NA),
         treated = ifelse(china.huawei > threshold, 1, treated)) %>%
  fill(treated))
dat$treated[is.na(dat$treated)==T] <- 0

sub = dat[dat$v2x_polyarchy>0.42,]
sub = sub[sub$cabb!="MNG",]
sub = sub[sub$cabb!="SSD",]
sub = sub[is.na(sub$v2smgovfilprc)==F & is.na(sub$treated)==F & is.na(sub$v2x_polyarchy)==F & is.na(sub$pres.election)==F & is.na(sub$coup.attempts)==F & is.na(sub$successful.coups)==F,]
sub = sub[is.na(sub$pcGDPcur)==F & is.na(sub$GDPcur)==F & is.na(sub$electricitypc)==F,]
nrow(dat);nrow(sub)

out1 <- gsynth(v2smgovfilprc ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=8, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out1, type="gap", xlim=c(-5,10), ylim=c(-0.2,1.0), main="Democracies\nSocial Media Filtering")
out1$est.avg
out1Periods8 = out1$est.avg

out2 <- gsynth(v2smgovshut ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=8, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out2, type="gap", xlim=c(-5,10), ylim=c(-0.2,1.0), main="Democracies\nInternet Shutdowns")
out2$est.avg
out2Periods8 = out2$est.avg

out3 <- gsynth(v2smgovsmmon ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=8, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out3, type="gap", xlim=c(-5,10), ylim=c(-0.2,1.0), main="Democracies\nSocial Media Monitoring")
out3$est.avg
out3Periods8 = out3$est.avg

out4 <- gsynth(v2smarrest ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=8, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out4, type="gap", xlim=c(-5,10), ylim=c(-0.2,1.0), main="Democracies\nArrests for Online Content")
out4$est.avg
out4Periods8 = out4$est.avg


# export table
ti = data.frame(rbind(out1Periods3,out1Periods4,out1Periods6,out1Periods7,out1Periods8))
ti$term = c("Transfer \u2265 $1,000,000 (Minimum Pre-Treatment Periods = 3)","Transfer \u2265 $1,000,000 (Minimum Pre-Treatment Periods = 4)","Transfer \u2265 $1,000,000 (Minimum Pre-Treatment Periods = 6)","Transfer \u2265 $1,000,000 (Minimum Pre-Treatment Periods = 7)","Transfer \u2265 $1,000,000 (Minimum Pre-Treatment Periods = 8)")
ti = ti[,c("term","Estimate","S.E.","p.value")]
names(ti) = c("term","estimate","std.error","p.value")
out1 = list(tidy=ti)
class(out1) = "modelsummary_list"

ti = data.frame(rbind(out2Periods3,out2Periods4,out2Periods6,out2Periods7,out2Periods8))
ti$term = c("Transfer \u2265 $1,000,000 (Minimum Pre-Treatment Periods = 3)","Transfer \u2265 $1,000,000 (Minimum Pre-Treatment Periods = 4)","Transfer \u2265 $1,000,000 (Minimum Pre-Treatment Periods = 6)","Transfer \u2265 $1,000,000 (Minimum Pre-Treatment Periods = 7)","Transfer \u2265 $1,000,000 (Minimum Pre-Treatment Periods = 8)")
ti = ti[,c("term","Estimate","S.E.","p.value")]
names(ti) = c("term","estimate","std.error","p.value")
out2 = list(tidy=ti)
class(out2) = "modelsummary_list"

ti = data.frame(rbind(out3Periods3,out3Periods4,out3Periods6,out3Periods7,out3Periods8))
ti$term = c("Transfer \u2265 $1,000,000 (Minimum Pre-Treatment Periods = 3)","Transfer \u2265 $1,000,000 (Minimum Pre-Treatment Periods = 4)","Transfer \u2265 $1,000,000 (Minimum Pre-Treatment Periods = 6)","Transfer \u2265 $1,000,000 (Minimum Pre-Treatment Periods = 7)","Transfer \u2265 $1,000,000 (Minimum Pre-Treatment Periods = 8)")
ti = ti[,c("term","Estimate","S.E.","p.value")]
names(ti) = c("term","estimate","std.error","p.value")
out3 = list(tidy=ti)
class(out3) = "modelsummary_list"

ti = data.frame(rbind(out4Periods3,out4Periods4,out4Periods6,out4Periods7,out4Periods8))
ti$term = c("Transfer \u2265 $1,000,000 (Minimum Pre-Treatment Periods = 3)","Transfer \u2265 $1,000,000 (Minimum Pre-Treatment Periods = 4)","Transfer \u2265 $1,000,000 (Minimum Pre-Treatment Periods = 6)","Transfer \u2265 $1,000,000 (Minimum Pre-Treatment Periods = 7)","Transfer \u2265 $1,000,000 (Minimum Pre-Treatment Periods = 8)")
ti = ti[,c("term","Estimate","S.E.","p.value")]
names(ti) = c("term","estimate","std.error","p.value")
out4 = list(tidy=ti)
class(out4) = "modelsummary_list"

myrows = data.frame(list(c("Country Fixed Effects",rep("Yes",4))
                         ,c("Year Fixed Effects",rep("Yes",4))
                         ,c("Control Variables",rep("Yes",4))
))

export = modelsummary(list("Internet Filtering" = out1
                           , "Internet Shutdowns" = out2
                           ,"Social Media Monitoring"=out3
                           ,"Arrests for Political Content"=out4
)
,add_rows=data.frame(t(myrows))
, stars=c('*' = .1, '**' = 0.05, '***' = .01)
, output="latex_tabular"
)
export = gsub("≥", "$\\\\ge$", export)
export

